<?php

namespace Constructor;

abstract class AbstractBuilder implements IBuilder
{
    public $aliases = array();
    protected $instances;
    
    public function __construct( Instances $instances = NULL ) { $this->instances = $instances; }
    public function get( $identifier = NULL ) { return $this->instances->get( $identifier ); }
    public function set( $instance, $identifier = NULL ) { return $this->instances->set( $instance, $identifier ); }
    public function has( $identifier ) 
    { 
        if( isset( $this->aliases[$identifier] ) ) return TRUE;
        return $this->instances->has( $identifier ); 
    }
    
    public function create( $identifier ) { return $this->instances->create( $identifier, $this->aliases ); }
    public function register( $alias, $identifier = NULL ) { $this->aliases[$alias] = $identifier; }
    public function getAlias( $identifier = NULL )
    {
        while( isset( $this->aliases[ $identifier ] ) AND $identifier !== $this->aliases[ $identifier ] )
        {
            $identifier = $this->aliases[ $identifier ];
        }
        
        return $identifier;
    }
    
    public function reset() { $this->instances->reset(); }
    public function count() { return $this->instances->count(); }
    public function getIdentifiers() { return $this->instances->getIdentifiers(); }
}
